package com.awspaas.user.apps.processforsap.event;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListener;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListenerInterface;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.sofa.common.profile.StringUtil;
import com.awspaas.user.apps.processforsap.util.HttpClientGwUtil;
import com.awspaas.user.apps.processforsap.util.HttpClientUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class GWTaskValidate extends InterruptListener implements InterruptListenerInterface {
    @Override
    public boolean execute(ProcessExecutionContext ctx) throws Exception {
        /**
         * 电子
         */
        boolean isty = ctx.isChoiceActionMenu("同意");

        if (isty) {
            //校验数据
            BO boMain = ctx.getBO("BO_EU_MM_GW");

            String pplx = boMain.getString("PPLX");
            String ckxhqk = boMain.getString("CKXH");
            String yycj = boMain.getString("YYCJ");
            String gn = boMain.getString("GN");
            String sffz = boMain.getString("SFFZ");
            String sfqg = boMain.getString("SFQG");
            String pp = boMain.getString("PP");
            String xh = boMain.getString("XH");
            String sflc = boMain.getString("SFLC");
            String rl = boMain.getString("RL");
            String jglx = boMain.getString("JGLX");
            String jzlx = boMain.getString("JZLX");
            String dlcs = boMain.getString("DLCS");
            String spmc= boMain.getString("SPMC");



            String sbpm = boMain.getString("SPBM");
            if (StringUtil.isNotEmpty(sbpm)) {
                if (sbpm.equals("8542319099") || sbpm.equals("8542339000") || sbpm.equals("8542399000")) {
                    if (StringUtil.isEmpty(pplx)||StringUtil.isEmpty(ckxhqk)||StringUtil.isEmpty(yycj)||StringUtil.isEmpty(gn)||StringUtil.isEmpty(sffz)||StringUtil.isEmpty(sfqg)||StringUtil.isEmpty(pp)||StringUtil.isEmpty(xh)||StringUtil.isEmpty(sflc)){
                        throw new BPMNError("0331","当商品编码为8542319099/8542339000/8542399000时，品牌类型|出口享惠情况|应用场景|功能|是否封装|是否切割|品牌|型号|是否量产-字段必填");
                    }
                }
                if (sbpm.equals("8542329010") || sbpm.equals("8542329090")) {
                    if (StringUtil.isEmpty(pplx)||StringUtil.isEmpty(ckxhqk)||StringUtil.isEmpty(yycj)||StringUtil.isEmpty(gn)||StringUtil.isEmpty(sffz)||StringUtil.isEmpty(sfqg)||StringUtil.isEmpty(pp)||StringUtil.isEmpty(xh)||StringUtil.isEmpty(sflc)||StringUtil.isEmpty(rl)){
                        throw new BPMNError("0331","当商品编码为8542319099/8542339000/8542399000时，品牌类型|出口享惠情况|应用场景|功能|是否封装|是否切割|品牌|型号|是否量产|容量-字段必填");
                    }
                }
                if (sbpm.equals("8532290000")){
                    if (StringUtil.isEmpty(pplx)||StringUtil.isEmpty(ckxhqk)||StringUtil.isEmpty(jglx)||StringUtil.isEmpty(jzlx)||StringUtil.isEmpty(pp)||StringUtil.isEmpty(xh)){
                        throw new BPMNError("0331","当商品编码为8542319099/8542339000/8542399000时，品牌类型|出口享惠情况|结构类型|介质类型|品牌|型号-字段必填");
                    }
                }
                if (sbpm.equals("8534009000")||sbpm.equals("8534001000")){
                    if (StringUtil.isEmpty(pplx)||StringUtil.isEmpty(ckxhqk)||StringUtil.isEmpty(dlcs)||StringUtil.isEmpty(boMain.getString("SFBDYJXYJ"))||StringUtil.isEmpty(boMain.getString("SFBDYYYDQYJ"))||StringUtil.isEmpty(pp)||StringUtil.isEmpty(xh)){
                        throw new BPMNError("0331","8534009000/8534001000/，品牌类型/出口享惠情况/电路层数/是否不带有机械元件/是否不带有有源电气元件/品牌(中文或外文名称)/型号-字段必填");

                    }
                }
            }



            //集成电路晶圆:集成电路晶圆(未封装圆片)|集成电路芯片:集成电路芯片(已封装IC)|硅电容器（未封装）:硅电容器（未封装）|基板:基板
            if (sbpm.equals("8532290000")){
                if (!"硅电容器（未封装）".equals(spmc)){
                    throw new BPMNError("0331","8532290000时，只能选择硅电容器（未封装）");
                }
            }
            if ("8534009000/8534001000".contains(sbpm)){
                if (!"基板".equals(spmc)){
                    throw new BPMNError("0331","8534009000/8534001000时，只能选择基板");
                }
            }
            if (!"8532290000/8534009000/8534001000".contains(sbpm)){
                if (!"集成电路芯片".equals(spmc)&&!"集成电路晶圆".equals(spmc)){
                    throw new BPMNError("0331","非8532290000/8534009000/8534001000情况下，只能集成电路晶圆或芯片");
                }
            }


//            String[] type = {"FH00", "FS00"};
//            if (boMain != null) {
//                String isBg = boMain.getString("IS_BG");
//                List<String> bonMars = new ArrayList<>();
//                if ("1".equals(isBg)) {
//                    bonMars.add("1");
//                } else {
//                    bonMars.add("1");
//                    bonMars.add("0");
//                }
//                for (String a : type) {
//                    for (String bondMar : bonMars) {
//                        Date date = new Date();
//                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                        String tokenUrl = "http://10.21.89.45:8099/pms/api/session";
//                        JSONObject jsonObject = new JSONObject();
//                        jsonObject.put("userno", a.equals("FH00") ? "3312964743-ERP" : "33129661F0-ERP");
//                        jsonObject.put("password", a.equals("FH00") ? "zhgw@123" : "zhgw@123");
//                        String result = HttpClientGwUtil.doPost(tokenUrl, jsonObject.toJSONString());
//                        JSONObject jsonResult = JSONObject.parseObject(result);
//                        if (jsonResult.getBoolean("success")) {
//                            JSONObject data = jsonResult.getJSONObject("data");
//                            String token = data.getString("accessToken");
//
//                            //开始调用业务代码
//                            String url = "http://10.21.89.45:8099/gw-edi-up/api/open/v1/erpMat/insertErpMat";
//                            JSONObject allData = new JSONObject();
//                            JSONArray dataInfo = new JSONArray();
//                            JSONObject jsonObject1 = new JSONObject();
//                            BO bo = ctx.getBO("BO_EU_MM_GW");
//                            //获取币种
//                            String bz = bo.getString("BZ");
//                            //获取物料组
//                            String matlGroup = bo.getString("MATL_GROUP");
//                            //获取商品编码
//                            String spbm = bo.getString("SPBM");
//                            if (StringUtil.isNotEmpty(spbm)) {
//                                if (spbm.contains("8542319099") || spbm.contains("8542339000") || spbm.contains("8542399000")) {
//                                    jsonObject1.put("gModel", bo.getString("PPLX") + "|3|" + bo.getString("YYCJ") + "|" + bo.getString("GN") + "|" + bo.getString("SFFZ") + "|" + bo.getString("SFQG") + "|" + bo.getString("PP") + "|" + bo.getString("XH") + "|" + bo.getString("SFLC"));
//                                } else if (spbm.contains("8542329010") || spbm.contains("8542329090")) {
//                                    jsonObject1.put("gModel", bo.getString("PPLX") + "|3|" + bo.getString("YYCJ") + "|" + bo.getString("GN") + "|" + bo.getString("SFFZ") + "|" + bo.getString("SFQG") + "|" + bo.getString("PP") + "|" + bo.getString("XH") + "|" + bo.getString("SFLC") + "|" + bo.getString("RL"));
//                                } else if (spbm.contains("8532290000")) {
//                                    jsonObject1.put("gModel", bo.getString("PPLX") + "|3|" + bo.getString("JGLX") + "|" + bo.getString("JZLX") + "|" + bo.getString("PP") + "|" + bo.getString("XH"));
//                                } else if (spbm.contains("8534009000") || spbm.contains("8534001000")) {
//                                    jsonObject1.put("gModel", bo.getString("PPLX") + "|" + bo.getString("DLCS") + "|" + bo.getString("YYCJ") + "|" + bo.getString("SFBDYJXYJ") + "|" + bo.getString("SFBDYYYDQYJ") + "|" + bo.getString("PP") + "|" + bo.getString("XH"));
//                                }
//                            }
//                            jsonObject1.put("bondMark", bondMar);
//                            String matlType = bo.getString("MATL_TYPE");
//
////                            jsonObject1.put("gMark", "1-IC-00".equals(matlGroup) ? "E" : "I");
//                            jsonObject1.put("gMark", "I");
//
//                            jsonObject1.put("facGNo", bo.getString("MATERIAL"));
//                            jsonObject1.put("copGNo", a.equals("FH00") ? bo.getString("LJ_YXDZ") : bo.getString("LJ_BDT"));
//                            jsonObject1.put("copGName", bo.getString("MATL_DESC"));
////                    jsonObject1.put("copGNameEn",);
//                            jsonObject1.put("copGModelEn", bo.getString("XH"));
//                            jsonObject1.put("codets", bo.getString("SPBM"));
////                            jsonObject1.put("createDepartment", "客服处");
//                            jsonObject1.put("unitErp", bo.getString("SBJLDW"));
//                            jsonObject1.put("supplierCode", bo.getString("KH"));
////                            jsonObject1.put("factory", bo.getString("PLANT"));
//                            jsonObject1.put("lastUpdateTime", simpleDateFormat.format(date));
//                            jsonObject1.put("createDepartment", ctx.getUserContext().getDepartmentModel().getName());
//                            jsonObject1.put("makeInsert", ctx.getUserContext().getUserName()+ctx.getUserContext().getUID());
//                            jsonObject1.put("gNo", "");
//                            dataInfo.add(jsonObject1);
//                            allData.put("data", dataInfo);
//                            String result1 = HttpClientGwUtil.doPost1(url, allData.toJSONString(), token);
//                        } else {
//                            throw new BPMNError("0331", "意外的接口调用错误！");
//                        }
//                    }
//                }
//            }

        }
        return true;
    }
}
